import { Login, UserInfoGet, getInfo } from '@/api/user'
import {
  removeTokentime,
  getTokentime,
  setTokentime,
  getToken,
  setToken,
  removeToken,
  getUserId,
  setUserId,
  removeUserId,
  getUserInfo,
  setUserInfo,
  removeUserInfo,
  getHeader,
  setHeader,
  removeHeader,
  getMenuList,
  setMenuList,
  removeMenuList
} from '@/utils/auth'
import router, { resetRouter } from '@/router'
import { OAuthLogin } from '@/api/index.js'
import { Message } from 'element-ui'
import { QueryUserMenu } from '@/api/settings/caidan'
import { generateRoutes } from '@/utils/ruoyi.js'

const state = {
  token: getToken(),
  tokentime: getTokentime(),
  userId: getUserId(),
  userInfo: getUserInfo() || {},
  MenuList: getMenuList() || [],
  name: '',
  avatar: '',
  introduction: '',
  roles: [],
  listHeader: getHeader()
}

const mutations = {
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_TOKENTIME: (state, time) => {
    state.tokentime = time
  },
  SET_USERID: (state, id) => {
    state.userId = id
  },
  SET_USERINFO(state, data) {
    state.userInfo = data
  },
  SET_Header: (state, list) => {
    state.listHeader = list
  },
  SET_INTRODUCTION: (state, introduction) => {
    state.introduction = introduction
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  SET_ROLES: (state, roles) => {
    state.roles = roles
  },
  SET_MenuList: (state, MenuList) => {
    state.MenuList = MenuList
  }
}

const actions = {
  // 登录
  async codeLogin({ commit }, userInfo) {
    const res = await OAuthLogin(userInfo)
    if (res.success) {
      // console.log('登录信息', res)
      const token = res.content
      const UserId = res.docid
      commit('SET_TOKEN', token)
      commit('SET_USERID', UserId)

      commit('SET_TOKENTIME', new Date().valueOf())
      setTokentime(new Date().valueOf())

      setUserId(UserId)
      setToken(token)

      const result = await UserInfoGet({ id: UserId })
      // console.log('人员信息', result.content[0])
      if (result.success) {
        const UserInfo = result.content[0]
        commit('SET_USERINFO', UserInfo)
        setUserInfo(UserInfo)
        const MenuRes = await QueryUserMenu({ id: UserInfo.id })
        // console.log('菜单信息', MenuRes)
        if (MenuRes.success) {
          const list = MenuRes.content
          commit('SET_MenuList', list)
          setMenuList(list)
        }
      }

      return token
    } else {
      Message({ message: res.message, type: 'warning' })
      throw new Error(res.message)
    }
  },
  async Login({ commit }, userInfo) {
    const res = await Login(userInfo)
    if (res.success) {
      // console.log('登录信息', res)
      const token = res.content
      const UserId = res.docid
      commit('SET_TOKEN', token)
      commit('SET_USERID', UserId)

      commit('SET_TOKENTIME', new Date().valueOf())
      setTokentime(new Date().valueOf())

      setUserId(UserId)
      setToken(token)

      const result = await UserInfoGet({ id: UserId })
      // console.log('人员信息', result.content[0])
      if (result.success) {
        const UserInfo = result.content[0]
        commit('SET_USERINFO', UserInfo)
        setUserInfo(UserInfo)
        const MenuRes = await QueryUserMenu({ id: UserInfo.id })
        // console.log('菜单信息', MenuRes)
        if (MenuRes.success) {
          const list = MenuRes.content
          commit('SET_MenuList', list)
          setMenuList(list)
        }
      }

      return token
    } else {
      Message({ message: res.message, type: 'warning' })
      throw new Error(res.message)
    }
  },

  // 退出
  logout({ commit }) {
    commit('SET_TOKEN', '')
    commit('SET_TOKENTIME', '')
    commit('SET_USERID', '')
    commit('SET_USERINFO', {})
    commit('SET_MenuList')
    removeToken()
    removeTokentime()
    removeUserId()
    removeUserInfo()
    removeMenuList()
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
